//
//  TelevisionLiveCollectionViewCell.m
//  Project_A Gary
//
//  Created by leilei on 16/3/28.
//  Copyright © 2016年 www.lei.com. All rights reserved.
//

#import "TelevisionLiveCollectionViewCell.h"
#import "TelevisionLiveTableViewCell.h"
#import "TelevisionLiveModel.h"
#import "DetailViewController.h"


@interface TelevisionLiveCollectionViewCell ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain)UITableView *tableV;
@property (nonatomic, retain)NSMutableArray *dataArr;


@end


@implementation TelevisionLiveCollectionViewCell

-(void)dealloc
{
    [_dataArr release];
    [_tableV release];
    [_navi release];

    
    [super dealloc];
}


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createTableView];
    }
    return self;
}


- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes
{
    [super applyLayoutAttributes:layoutAttributes];
    [self dataHandle];
}

- (void)dataHandle
{
    self.dataArr=[NSMutableArray array];
    NSString *str = @"http://mrobot.pcauto.com.cn/v2/cms/channels/18?v=4.0.0&pageNo=1&pageSize=20";
    [NetworkHandle GetDataWithURL:str dic:nil success:^(id responseObject) {
        self.dataArr = [TelevisionLiveModel transformWithArray:responseObject[@"data"]];
        [_tableV reloadData];
    } filed:^(NSError *error) {
        
    }];
}

- (void)createTableView
{
    self.tableV = [[UITableView alloc] initWithFrame: CGRectMake(0, 0, BOUNDS_WIDTH, BOUNDS_HEIGHT - 64 - 49 - 40) style:UITableViewStylePlain];
    self.tableV.backgroundColor = [UIColor grayColor];
    self.tableV.delegate = self;
    self.tableV.dataSource = self;
    [self.contentView addSubview:self.tableV];
    self.tableV.rowHeight = 290;
    [self.tableV registerClass:[TelevisionLiveTableViewCell class] forCellReuseIdentifier:@"TelevisionLiveTableViewCell"];
    
    [_tableV release];
}




#pragma mark - 点击进入详情方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DetailViewController *detail = [[DetailViewController alloc] init];
    TelevisionLiveModel *televisionModel = self.dataArr[indexPath.row];
    detail.url = televisionModel.zbUrl;
    [self.navi pushViewController:detail animated:YES];
    
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TelevisionLiveTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TelevisionLiveTableViewCell" forIndexPath:indexPath];
    TelevisionLiveModel *televisionM = self.dataArr[indexPath.row];
    cell.backgroundColor = [UIColor colorWithRed:236 / 255.0 green:236 / 255.0 blue:236 / 255.0 alpha:1];
    cell.titleLab.text = televisionM.titile;
    cell.countLab.text = [NSString stringWithFormat:@"评论数%@", televisionM.count];
    cell.timeLab.text = televisionM.zbTime;
    
    if ([televisionM.zbState isEqualToString:@"0"]) {
        cell.stateLab.text = @"已结束";
    } else{
        cell.stateLab.text = @"正直播";
    }
    
    [cell.imgView sd_setImageWithURL:[NSURL URLWithString:televisionM.banner]];
    
    
    
    return cell;
}

@end
